import { childRoutes } from "@/router/router";

const GLOBAL = {
	CLEAR_STATE: "clearState",
	SET_USER: "setUser",
	SET_USER_TOKEN: "setUserToken"
};

const initialState = {
	token: "",
	user: {}
};
const state = () => ({ ...initialState });

const getters = {};

const mutations = {
	[GLOBAL.CLEAR_STATE](state) {
		Object.assign(state, initialState);
	},
	[GLOBAL.SET_USER](state, data) {
		state.user = data;
	},
	[GLOBAL.SET_USER_TOKEN](state, data) {
		state.token = data;
	}
};

const actions = {
	// 清除数据
	clearState({ commit }) {
		commit(GLOBAL.CLEAR_STATE);
	},
	// 设置用户信息
	setUser({ commit, dispatch }, data) {
		commit(GLOBAL.SET_USER, data);
		// 根据用户信息设置菜单
		dispatch("menu/setMenu", childRoutes, { root: true });
	},
	// 设置token
	setUserToken({ commit }, data) {
		commit(GLOBAL.SET_USER_TOKEN, data);
	}
};

export default {
	actions,
	getters,
	mutations,
	namespaced: true,
	state
};
